import 'package:elephant_fish/http/core/hi_net.dart';
import 'package:elephant_fish/http/request/base_request.dart';
import 'package:elephant_fish/http/request/family_delete_user_request.dart';
import 'package:elephant_fish/http/request/family_details_request.dart';
import 'package:elephant_fish/http/request/user_info_request.dart';
import 'package:elephant_fish/model/family_entity.dart';
import 'package:elephant_fish/model/family_member_entity.dart';
import 'package:elephant_fish/model/user_info_entity.dart';
import 'package:elephant_fish/page/login_page.dart';
import 'package:elephant_fish/util/TxUtils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:elephant_fish/util/Alert.dart';
import 'package:shared_preferences/shared_preferences.dart';

class FamilyMember extends StatefulWidget {
  final FamilyMemberEntity familyEntity;
  const FamilyMember({Key key, this.familyEntity}) : super(key: key);

  @override
  _FamilyMemberState createState() => _FamilyMemberState();
}

class _FamilyMemberState extends State<FamilyMember> {
  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        SizedBox(width: 18,),
        InkWell(
          child: Container(
            width: 40,
            height: 40,
            child: CircleAvatar(
              radius: 25,
              backgroundImage: NetworkImage('http://119.28.131.94:82' + widget.familyEntity.avatar),
            ),
          ),
          onTap: (){
            _pickMember();
          },
        ),
        SizedBox(width: 9.5,),
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(widget.familyEntity.nickname,style: TextStyle(color: Color(0xffffffff),fontSize: 15),),
            SizedBox(height: 6,),
            Text('ID:' + widget.familyEntity.idNumber,style: TextStyle(color: Color(0x70ffffff),fontSize: 13),),
          ],
        ),
        SizedBox(width: 120,),
           Text('贡献:' + widget.familyEntity.contribution.toString(),style: TextStyle(color: Color(0xffffffff),fontSize: 15),),

      ],
    );
  }

  ///底部弹框
  _pickMember() {
    showModalBottomSheet(context: context, builder: (context) =>
        Container(
          height: 160,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              _takePhotoItem('删除家族成员', 0),
              _takePhotoItem('取消', 1),
            ],
          ),
        ));
  }

  _takePhotoItem(String title, int num) {
    return GestureDetector(
      child: ListTile(
        title: Center(
          child: Text(title,style: TextStyle(color: num == 0?Color(0xffff215d):Color(0xff343434)),),
        ),
        onTap: () => num == 0 ? showJoinDialog() : Navigator.pop(context),
      ),
    );
  }

  Future<void> showJoinDialog() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String appToken = prefs.getString('app-token');
    Alert.showCustomDialog(
        context: context,
        title: '删除族成员',
        message: '您确定要加入'+ widget.familyEntity.nickname + '(ID:'+ widget.familyEntity.idNumber + ')' +'从家族中移除吗',
        certainPressed: () async {
          BaseRequest request = FamilyDeleteUserRequset();
          request.add('family_id', widget.familyEntity.familyId).add('user_id', widget.familyEntity.id).addHeader('usertoken', appToken);
          var result = await HiNet.getInstance().fire(request);
          if (result['code'] == 0) {
            TxUtils.showToast('删除成功', context);

          } else {
            TxUtils.showToast(result['message'], context);
          }
        });
  }


}
